// var pguid = null;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'csg/cschedulereport/list?state=isdelno&sGuid='+GetUrlParam("sGuid"),
        datatype: "json",
        colModel: [
            { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
            { label: '项目名称', name: 'pGuid', index: 'p_guid', width: 80,hidden:true },
            { label: '所属进度id', name: 'sGuid', index: 's_guid', width: 80,hidden:true },
            { label: '所属进度名称', name: 'sName', index: 's_name', width: 80,hidden:true },
            { label: '记录主题', name: 'sRTheme', index: 's_r_theme', width: 80 },
            { label: '记录时间', name: 'sRAddtime', index: 's_r_addtime', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            { label: '记录详情', name: 'sRDetails', index: 's_r_details', width: 80 },
            { label: '填报人员名称', name: 'sRUsername', index: 's_r_username', width: 80 },
            { label: '填报人员id', name: 'sRUser', index: 's_r_user', width: 80,hidden:true },
            { label: '更新时间', name: 'sRUpdatetime', index: 's_r_updatetime', width: 80,hidden:true,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            { label: '状态', name: 'sRDatastate', index: 's_r_datastate', width: 80,hidden:true }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        cScheduleReport: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.cScheduleReport = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }

            var rowData = $("#jqGrid").jqGrid('getRowData',guid);
            var pguid = rowData.pGuid;
            // $.get(baseURL + "csg/cpbase/select?pname="+pname, function(r) {
            //     pguid=r.returnlist[0].guid;

                $.get(baseURL + "csg/cpbase/info/"+pguid, function(r) {
                    if (r.cPBase.pBState == 30) {
                        alert("项目已归档，无法修改记录信息！");
                    }else{
                        vm.showList = false;
                        vm.title = "修改";

                        vm.getInfo(guid)
                    }
                });

            // });

        },
        saveOrUpdate: function (event) {
            var sGuid = GetUrlParam("sGuid");
            var pGuid = GetUrlParam("pGuid");

            var url = vm.cScheduleReport.guid == null ? "csg/cschedulereport/saveNew/"+sGuid+"/"+pGuid: "csg/cschedulereport/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.cScheduleReport),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }
            var guid = guids[0];
            var rowData = $("#jqGrid").jqGrid('getRowData',guid);
            var pguid = rowData.pGuid;
            // $.get(baseURL + "csg/cpbase/select?pname="+pname, function(r) {
            //     pguid=r.returnlist[0].guid;

                $.get(baseURL + "csg/cpbase/info/"+pguid, function(r) {
                    if (r.cPBase.pBState != 30) {

                        confirm('确定要删除选中的记录？', function(){
                            $.ajax({
                                type: "POST",
                                url: baseURL + "csg/cschedulereport/delete",
                                contentType: "application/json",
                                data: JSON.stringify(guids),
                                success: function(r){
                                    if(r.code == 0){
                                        alert('操作成功', function(index){
                                            $("#jqGrid").trigger("reloadGrid");
                                        });
                                    }else{
                                        alert(r.msg);
                                    }
                                }
                            });
                        });
                    }else{
                        alert("项目已归档，无法删除记录信息！")
                    }
                })
            // });
        },
        getInfo: function(guid){
            $.get(baseURL + "csg/cschedulereport/info/"+guid, function(r){
                vm.cScheduleReport = r.cScheduleReport;
                vm.cScheduleReport.sRAddtime = r.cScheduleReport.sRAddtime != null?new Date().Format("yyyy-MM-dd"):"";
                vm.cScheduleReport.sRUpdatetime = r.cScheduleReport.sRUpdatetime != null?new Date().Format("yyyy-MM-dd"):"";
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});